package com.sunda.spmswms.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmswms.mapper.WhsReportMapper;
import com.sunda.spmswms.service.IWhsReport;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class WhsReport implements IWhsReport {

    @Autowired
    WhsReportMapper whsReportMapper;

    /**
     * 按照物料编码,需求单号,箱码,包装单号,采购定单号,送货单号,内部交货单号等查询箱码信息
     * */
    @Override
    public ResponseResult queryBoxInfo(JSONObject queryObj) {
        if(null==queryObj) return ResponseResult.error("Please provide query parmas");
        String boxNote=StringUtils.isEmpty(queryObj.getString("boxNote"))?null:queryObj.getString("boxNote");
        String materialNo=StringUtils.isEmpty(queryObj.getString("materialNo"))?null:queryObj.getString("materialNo");
        String demandNo=StringUtils.isEmpty(queryObj.getString("demandNo"))?null:queryObj.getString("demandNo");
        String packageNote=StringUtils.isEmpty(queryObj.getString("packageNote"))?null:queryObj.getString("packageNote");
        String purchaseNo=StringUtils.isEmpty(queryObj.getString("purchaseNo"))?null:queryObj.getString("purchaseNo");
        String deliveryNoteSap=StringUtils.isEmpty(queryObj.getString("deliveryNoteSap"))?null:queryObj.getString("deliveryNoteSap");
        String srmContractNo=StringUtils.isEmpty(queryObj.getString("srmContractNo"))?null:queryObj.getString("srmContractNo");
        return ResponseResult.success().add("boxInfo",whsReportMapper.queryBoxInfo(boxNote,materialNo,demandNo,packageNote,purchaseNo,deliveryNoteSap,srmContractNo));
    }

    /*出库 -- 贸易货导出 -- 装柜清单*/
    @Override
    public ResponseResult outWHExportM(JSONObject querObj) {
        String packageList=StringUtils.isEmpty(querObj.getString("packageList"))?null:querObj.getString("packageList");
        String cabinetNumber=StringUtils.isEmpty(querObj.getString("cabinetNumber"))?null:querObj.getString("cabinetNumber");
        if(StringUtils.isEmpty(packageList) && StringUtils.isEmpty(cabinetNumber)){
            return ResponseResult.error("packageList and cabinetNumber are all blank");
        }

        return ResponseResult.success().add("outWHExportM",whsReportMapper.outWHExportM(cabinetNumber,packageList));
    }

    /*出库 -- 备件货导出 -- 装柜清单*/
    @Override
    public ResponseResult outWHExportX(JSONObject querObj) {
        String packageList=StringUtils.isEmpty(querObj.getString("packageList"))?null:querObj.getString("packageList");
        String cabinetNumber=StringUtils.isEmpty(querObj.getString("cabinetNumber"))?null:querObj.getString("cabinetNumber");
        if(StringUtils.isEmpty(packageList) && StringUtils.isEmpty(cabinetNumber)){
            return ResponseResult.error("packageList and cabinetNumber are all blank");
        }
        
        return ResponseResult.success().add("outWHExportX",whsReportMapper.outWHExportX(cabinetNumber,packageList));
    }

    @Override
    public ResponseResult inHouseDtlExport(JSONObject queryObj,String goodsType) {
        String werks=StringUtils.isEmpty(queryObj.getString("werks"))?null:queryObj.getString("werks");
        String whsLocationCode=StringUtils.isEmpty(queryObj.getString("whsLocationCode"))?null:queryObj.getString("whsLocationCode");
        String deliveryNoteSap=StringUtils.isEmpty(queryObj.getString("deliveryNoteSap"))?null:queryObj.getString("deliveryNoteSap");
        String startDate=queryObj.getString("startDate");
        String endDate=queryObj.getString("endDate");

        if(StringUtils.isEmpty(werks) || StringUtils.isEmpty(whsLocationCode)){
            return ResponseResult.error("Werks and WhsLocationCode are mandatory required");
        }

        if(StringUtils.isEmpty(startDate) || StringUtils.isEmpty(endDate)){
            return ResponseResult.error("Date period is mandatory required");
        }
        if(goodsType.equals("X")){
            return ResponseResult.success().add("inHouseDtlExportX",whsReportMapper.inHouseDtlExportX(werks,whsLocationCode,startDate,endDate,deliveryNoteSap)) ;
        }else {
            return ResponseResult.success().add("inHouseDtlExportM", whsReportMapper.inHouseDtlExportM(werks, whsLocationCode, startDate, endDate, deliveryNoteSap));
        }
    }

    @Override
    public ResponseResult outHouseDtlExport(JSONObject queryObj,String goodsType) {
        String werks=StringUtils.isEmpty(queryObj.getString("werks"))?null:queryObj.getString("werks");
        String whsLocationCode=StringUtils.isEmpty(queryObj.getString("whsLocationCode"))?null:queryObj.getString("whsLocationCode");
        String packageList=StringUtils.isEmpty(queryObj.getString("packageList"))?null:queryObj.getString("packageList");
        String startDate=queryObj.getString("startDate");
        String endDate=queryObj.getString("endDate");
        if(StringUtils.isEmpty(werks) || StringUtils.isEmpty(whsLocationCode)){
            return ResponseResult.error("Werks and WhsLocationCode are mandatory required");
        }

        if(StringUtils.isEmpty(startDate) || StringUtils.isEmpty(endDate)){
            return ResponseResult.error("Date period is mandatory required");
        }
        if(goodsType.equals("X")){
            return ResponseResult.success().add("outHouseDtlExportX",whsReportMapper.outHouseDtlExportX(werks,whsLocationCode,startDate,endDate,packageList)) ;
        }else {
            return ResponseResult.success().add("outHouseDtlExportM", whsReportMapper.outHouseDtlExportM(werks, whsLocationCode, startDate, endDate, packageList));
        }
    }

}
